﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;
namespace wxx.mobile
{
    /// <summary>
    /// wxx_mobile1 的摘要说明
    /// </summary>
    public class wxx_mobile1 : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["init1"] != null)
            {
                string str = readlist1("", context);
                if (str == "") { context.Response.Write("false"); }
                else
                {

                    context.Response.Write(str);
                }
            }

           
            if (context.Request.QueryString["mbedit1"] != null)
            {
                String str = readedit1(context.Request.QueryString["mbedit1"].ToString(), context);
                context.Response.Write(str);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string readlist1(string cpmingcheng, HttpContext context)
        {
            wxx.BLL.t_order obll = new wxx.BLL.t_order();
            wxx.Model.t_order omodel = new wxx.Model.t_order();
            String ReaderId=context.Session["user"].ToString();
            string strif = " order by id desc";
            DataSet ds1 = new DataSet();
            if (cpmingcheng != "")//
                ds1 = obll.GetList("BookName like'%" + cpmingcheng + "%'");
            // strif = " where BookName like '%" + cpmingcheng + "%' order by id desc";
               
            else//搜索框为空就出现全部数据
            {
                ds1 = obll.GetList("ReaderId='"+ReaderId+"'");
            }
            string strret = "";

            if (ds1.Tables[0].Rows.Count > 0)
            {
                strret = "<table class='table' style='margin-top:10px'>\r\n";

                strret += "<thead><tr><th >读者iD</th><th >书籍编号</th><th scope='col'>书籍名称</th><th >预约时间</th><th>状态</th></tr></thead><tbody>\r\n";

                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    String State = ds1.Tables[0].Rows[i]["State"].ToString();
                    if (i % 2 == 0)
                        if (State == "已预约" || State == "可借阅")
                            strret += "<tr> <td class='success'>" + ds1.Tables[0].Rows[i]["ReaderId"].ToString() + "</td> <td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["OrderTime"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["State"].ToString() + "</td><td><button type='submit' class='btn btn-default' style=' width:100px;' onclick=\"order1('" + ds1.Tables[0].Rows[i]["id"].ToString() + "');\">取消预约</button></td> </tr>\r\n";
                        else {
                            strret += "<tr> <td class='success'>" + ds1.Tables[0].Rows[i]["ReaderId"].ToString() + "</td> <td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["OrderTime"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["State"].ToString() + "</td> </tr>\r\n";
                        
                        }
                    else
                        if (State == "已预约" || State == "可借阅")
                        strret += "<tr class='warning'> <td>" + ds1.Tables[0].Rows[i]["ReaderId"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["OrderTime"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["State"].ToString() + "</td> <td><button type='submit' class='btn btn-default' style=' width:100px;' onclick=\"order1('" + ds1.Tables[0].Rows[i]["id"].ToString() + "'); \">取消预约</button></td> </tr>\r\n";
                        else           
                    strret += "<tr> <td class='warning'>" + ds1.Tables[0].Rows[i]["ReaderId"].ToString() + "</td> <td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["OrderTime"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["State"].ToString() + "</td> </tr>\r\n";
                }
                strret += "</tbody></table>\r\n";
                return strret;
            }
            else
            {

                return strret;
            }
        }
        private String readedit1(string myid, HttpContext context)
        {
            wxx.BLL.t_order obll = new wxx.BLL.t_order();
            wxx.Model.t_order omodel = new wxx.Model.t_order();
            omodel = obll.GetModel(int.Parse(myid));
            String State = omodel.State;
           String ISBN=omodel.ISBN;
                omodel.State = "已取消";
                omodel.OrderTime = DateTime.Now;
                obll.Update1(omodel);
                if (State == "可借阅")
                {
                    //一本图书借阅表中书本状态变成未借出
                    wxx.BLL.t_librarybookinfo lbibll = new wxx.BLL.t_librarybookinfo();
                    wxx.Model.t_librarybookinfo lbimodel = new wxx.Model.t_librarybookinfo();
                    DataSet lbids = lbibll.GetList("ISBN='" + ISBN + "'");
                    String Idlbi = lbids.Tables[0].Rows[0][0].ToString();
                    lbimodel = lbibll.GetModel(int.Parse(Idlbi));
                    lbimodel.BookState = "未借出";
                    lbibll.Update1(lbimodel);
                }

            return "true";

        }
    }
}